package com.thirtydays.frame.util

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.thirtydays.frame.BuildConfig
import com.thirtydays.frame.R

/**
 * @创建者:小垚
 * @时间:2021/9/26
 *@描述:
 */
object IntentUtils {
    /**
     * 跳转浏览器
     */
    fun openIntentUrl(context: Context, url: String) {
        var intent = Intent().apply {
            action = Intent.ACTION_VIEW
            data = Uri.parse(url)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        if (intent.resolveActivity(context.packageManager) != null) {
            context.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_browser)))
        } else {
            ToastUtils.showShort(R.string.url_error)
        }
    }

    /**
     * 跳转应用市场
     */
    fun toMarket(context: Context) {
        try {
            val parse = Uri.parse("market://details?id=${AppUtils.getAppPackageName()}")
            context.startActivity(Intent(Intent.ACTION_VIEW, parse).apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
        } catch (e: Exception) {
            ToastUtils.showShort("您的手机没有安装Android应用市场,无法评分")
        }
    }

    /**
     * 跳转谷歌市场
     */
    const val googlePlay = "com.android.vending"
    fun transferToGooglePlay(context: Context) {
        try {
            val uri = Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID)
            val intent = Intent(Intent.ACTION_VIEW, uri)
            intent.setPackage(googlePlay)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        } catch (e: Exception) {
            ToastUtils.showShort(e.toString())
        }
    }
}